<?php

namespace app\admin\controller;

use think\Request;
use think\facade\Db;
use app\common\model\AuthRule;
use app\common\model\User;


class UserController extends BaseController
{

    /**
     * 用户列表
     * @params string
     * @return
     */
    public function index(Request $request)
    {
        if($this->method == 'POST')
        {
            $where = $request->post();
            $query = User::alias('a');
            if(!empty($where['username']))
            {
                $query->whereLike('username', '%'.$where['username'].'%');
            }
            if(!empty($where['phone']))
            {
                $query->where('phone', $where['phone']);
            }
            if(!empty($where['sex']))
            {
                $query->where('sex', $where['sex']);
            }
            if(is_numeric($where['status']))
            {
                $query->where('status', $where['status']);
            }
            $total = $query->count('a.id');
            $list  = $query->order('a.id','desc')->page($this->page, $this->limit)->select()->toArray();

            return returnData($list, $total);
        }else{
            return view('user/user_list');
        }

    }

    /**
     * 添加
     * @params string
     * @return
     */
    public function add(Request $request)
    {
        if($this->method == 'POST')
        {
            $data = $request->post();
            if(empty($data['username']))
            {
                return returnFail('请填入用户名称');
            }
            if(User::where('username',$data['username'])->find())
            {
                return returnFail('用户名称已存在');
            }
            if(User::where('phone',$data['phone'])->find())
            {
                return returnFail('手机号已存在');
            }
            $res = User::create($data);
            if($res)
            {
                return returnSuccess('添加成功');
            }
            return returnFail('添加失败');
        }else{
            return view('user/add_user');
        }
    }

    /**
     * 修改
     * @params string
     * @return
     */
    public function edit(Request $request)
    {
        if($this->method == 'POST')
        {
            $data = $request->post();
            if(empty($data['username']))
            {
                return returnFail('请填入用户名称');
            }
            if(User::where('username',$data['username'])->where('id','<>', $data['id'])->find())
            {
                return returnFail('用户名称已存在');
            }
            if(User::where('phone',$data['phone'])->where('id','<>', $data['id'])->find())
            {
                return returnFail('手机号已存在');
            }
            $res = User::update($data, ['id'=>$data['id']]);
            if($res)
            {
                return returnSuccess('修改成功');
            }
            return returnFail('修改失败');
        }else{
            $assign['info'] = User::find($request->get('id'));
            return view('user/edit_user', $assign);
        }
    }

    /**
     * 删除
     * @params string
     * @return
     */
    public function del(Request $request)
    {
        if($this->method == 'POST')
        {
            $id = $request->post('id');

            User::destroy($id,true);   //真删除

            return returnSuccess('删除成功');
        }
        return returnFail('非法请求');
    }

}
